<?php

$this->AddHook('other_work', 'Send');

function SendObject($bot, $obj) {
//	$SendPlugin = new SendPlugin();
//	$fname = ' - '.$SendPlugin->flN((string)$value['itemName']);
	$fname = '';
    $bot->SendMsg('[SendTo] Moving item id: '.$obj['id'].' - '.$obj['itemName'].$fname);
    $amf = new AMFObject("");
    $amf->_bodys[0] = new MessageBody();
    $amf->_bodys[0]->_value[0] = $bot->GetAmfHeader();
    $amf->_bodys[0]->targetURI = 'BaseService.dispatchBatch';
    $amf->_bodys[0]->responseURI = '/1';
    $amf->_bodys[0]->_value[2] = 0;
    $amf->_bodys[0]->_value[1][0]['sequence'] = $bot->GetSequense();
    $amf->_bodys[0]->_value[1][0]['functionName'] = "WorldService.performAction";
    $amf->_bodys[0]->_value[1][0]['params'][0] = "sendToInventory";
    $amf->_bodys[0]->_value[1][0]['params'][1] = $obj;
    $amf->_bodys[0]->_value[1][0]['params'][2] = Array();
    $serializer = new AMFSerializer();
    $result = $serializer->serialize($amf);
    $x = $bot->SendRequest($result);
	$bot->Add2Report('SendTo Inventory');
    return $x;
}
	
function Send($bot) {
	if ($bot->firstrun)
	{ //not work if it's first bot cycle
		$Name = "Sendplugin"; $Version = "1.0"; $Date = "18.04.2112";
		$bot->ld->UpdatePluginVersion($bot, $Name, $Version, $Date )  ;
		return;
	}

	$msg = $bot->irc('u') . $bot->irc('b');
	$msg .= $bot->irc('cc') . '2';
	$msg .= $bot->irc('ff') . '13,5' . chr(232) . $bot->irc('ff') . '14,5';
	$msg .= 'Starting Plugin: SendTo Inventory';
	$bot->SendMsg($msg);

    $data = $bot->ld->GetPlSettings("Sendplugin");
    $stop = "Y";
    if (!isset($data->Send))  { $data->pause = 1;  $bot->ld->SavePlSettings("Sendplugin", $data); return;}
    if ($data->Send == 0  ) { $bot->SendMsg('Send: no action to be done'); return;}
    if (!isset($data->itemList)) {$itemList = 0; }
    if (empty($data->itemList)) {$itemList = 0; }
    $itemList    = $data->itemList;
    if (!isset($data->dt_itemList)) {$dt_itemList = 0; }
    if (empty($data->dt_itemList)) {$dt_itemList = 0; }
    $dt_itemList    = $data->dt_itemList;
    if(($itemList == 0) && ($dt_itemList == 0))
           {
             $bot->SendMsg('[SendTo] Plugin Send: Nothing to do..');
             return;
           }

    $data->Send = 0; // reset itemid for next cycle
    $data->itemList = 0;
	$data->dt_itemList = 0;
    $bot->ld->SavePlSettings("Sendplugin", $data);
   
    // start of the plugin doing work
    $bot->SendMsg('[SendTo] *******************************');
    $bot->SendMsg('[SendTo] Start SendToInventory plugin');
    $now = time();

    $bot->SendMsg('[SendTo] *******************************');
    
	if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
    $ReloadN = 50;
    while($ReloadN > 0)
    {
        $res=$bot->ld->GetSelect("select * from objects");
        if(count($res) > 10)
          {
            $bot->SendMsg("[SendTo] *** Working on ***");
            $ReloadN = 0;
          }
          else { $bot->SendMsg("[SendTo] *** Ops... there is an error  $ReloadN /50*");  return;}
          $ReloadN--;
    }
	foreach ($res as $val)
            {
            $v=(string)$val[1];
            $obj=unserialize(base64_decode($v));
			$exist = in_array($obj['id'],$itemList);
            if($exist)
              {
                SendObject($bot, $obj);
             }
          }
    if (isset($bot->error_msg)) {  $bot->SendMsg('[SendTo] Exit by Error');   }

	if ($bot->currentWorld != "world_downtown") $bot->LoadWorldObjects("world_downtown");
    $ReloadN = 50;
    while($ReloadN > 0)
    {
        $res=$bot->ld->GetSelect("select * from dt_objects");
        if(count($res) > 10)
          {
            $bot->SendMsg("[SendTo] *** Working on ***");
            $ReloadN = 0;
          }
          else { $bot->SendMsg("[SendTo] *** Ops... there is an error  $ReloadN /50*");  return;}
          $ReloadN--;
    }
	foreach ($res as $val)
            {
            $v=(string)$val[1];
            $obj=unserialize(base64_decode($v));
			$exist = in_array($obj['id'],$dt_itemList);
            if($exist)
              {
                SendObject($bot, $obj);
             }
          }
    if (isset($bot->error_msg)) {  $bot->SendMsg('[SendTo] Exit by Error');   }

    $bot->SendMsg('[SendTo] End SendToInventory plugin');
	if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main"); // set current world back to Main.. less chance of breaking other plugins.
    $bot->ReloadConfig();
	$bot->pm->RefreshMePlugin("sendTo");

} // end function




?>